import numpy as np

a=list()
a = [int(x) for x in input().split()]

ans = np.zeros((a[0],a[0]))

arr = [[0]*a[0]]*a[0]
for i in range(a[0]):
    arr[i] = input().split(" ")
arr = np.array(arr)
print(arr,type(arr))


def  test(arr1,i):
    arr2 = arr1
    for i in range(1,i+1):
        arr2 *= arr1
    return arr2



for i in range(1,a[1]+1):
    ans += test(arr,i)

for i in range(a[0]):
    for j in range(a[0]):
        print(ans[i][j]%a[2])







